route.test.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import { describe, it, expect, beforeAll, afterAll } from "vitest";
  2. import fs from "node:fs/promises";
  3. import os from "node:os";
  4. import path from "node:path";
  5. import { GET as getMonths } from "./route.js";
  6. let tmpRoot;
  7. beforeAll(async () => {
  8. tmpRoot = await fs.mkdtemp(path.join(os.tmpdir(), "api-months-"));
  9. process.env.NAS_ROOT_PATH = tmpRoot;
  10. // tmpRoot/NL01/2024/10
  11. await fs.mkdir(path.join(tmpRoot, "NL01", "2024", "10"), {
  12. recursive: true,
  13. });
  14. });
  15. afterAll(async () => {
  16. await fs.rm(tmpRoot, { recursive: true, force: true });
  17. });
  18. describe("GET /api/branches/[branch]/[year]/months", () => {
  19. it("returns months for a valid branch/year", async () => {
  20. const req = new Request("http://localhost/api/branches/NL01/2024/months");
  21. const ctx = {
  22. params: Promise.resolve({ branch: "NL01", year: "2024" }),
  23. };
  24. const res = await getMonths(req, ctx);
  25. expect(res.status).toBe(200);
  26. const body = await res.json();
  27. expect(body).toEqual({
  28. branch: "NL01",
  29. year: "2024",
  30. months: ["10"],
  31. });
  32. });
  33. it("returns 400 when branch or year is missing", async () => {
  34. const req = new Request("http://localhost/api/branches/NL01/2024/months");
  35. const ctx = {
  36. params: Promise.resolve({ branch: "NL01" }), // year missing
  37. };
  38. const res = await getMonths(req, ctx);
  39. expect(res.status).toBe(400);
  40. const body = await res.json();
  41. expect(body.error).toBe("branch oder year fehlt");
  42. });
  43. });